# coding=utf-8

import os
import time
import threading
import multiprocessing

def worker(sign, lock):
    lock.acquire()
    print sign, os.getpid()
    lock.release()

def worker2(sign, lock):
    # lock.acquire()
    while True:
        time.sleep(2)
        print sign, os.getpid()
    # lock.release()
# Main
print "Main process:", os.getpid()

# Multi-thread
record = []
lock = threading.Lock()
for i in range(5):
    thread = threading.Thread(target=worker, args=("thread", lock))
    thread.start()
    record.append(thread)

for t in record:
    t.join()


# Multi-process
record = []
lock = multiprocessing.Lock()
for i in range(5):
    process = multiprocessing.Process(target=worker2, args=("process", lock))
    process.start()
    record.append(process)

for p in record:
    p.join()

